const service = require('../services/deploy');
const { logger } = require('../services/logger');
/**
 * use http protocol deployed contracts
 * add by: martin
 * date:2018-06-30
 * params.name: token name
 * params.symbol:token symbol
 * params.decimals:token decimals
 * params.total: init token total 
 */
let deployedContract = async (ctx, next) => {
    try {

        let params = ctx.query
        if (params.name == undefined || params.symbol == undefined || params.decimals == undefined || params.total == undefined) {
            ctx.body = { status: "0000", errCode: "10001", msg: 'params is be requeired' }
            return;
        } //name=MT&symbol=MTC&decimals=18&total=12000000
        logger.info('params', params);
        let result = await service.deployed(params);

        ctx.body = { status: "0000" }
        ctx.body.data = result;
    } catch (error) {
        logger.error("deployedContract error===>", error);
    }
    next();
}

// /**
//  * use http protocol getBalance of ERC20 tokens
//  * add by: martin
//  * date:2018-07-12
//  * ctrt_address: address of contractx
//  */
// let getBalance = async (ctx, next) => {
//     try {

//         let ctrt_address = ctx.query.ctrt_address
//         if (ctrt_address == undefined) {
//             ctx.body = { status: "0000", errCode: "10001", msg: 'contract address is be requeired' }
//             return;
//         }
//         console.log('ctrt_address', ctrt_address);
//         let result = await service.getBalance(ctrt_address);

//         ctx.body = { status: "0000" }
//         ctx.body.data = result;
//     } catch (error) {
//         console.log("getBalance error===>", error);
//     }
//     next();
// }

// /**
//  * use http protocol transfer to address of contract,not transfer to address of ethereum 
//  * add by: martin
//  * date:2018-07-12
//  * ctrt_address: address of contractx
//  * to_address: address of contractx 
//  * amount: transfer to amount 
//  */
// let transfer = async (ctx, next) => {
//     try {

//         let to_address = ctx.query.to_address,
//             ctrt_address = ctx.query.to_address,
//             amount = ctx.query.amount;
//         if (to_address == undefined || amount == undefined || ctrt_address == undefined) {
//             ctx.body = { status: "0000", errCode: "10001", msg: 'address is be requeired' }
//             return;
//         }
//         console.log('ctx.query', ctx.query);
//         let result = await service.transfer(ctrt_address,to_address, amount);

//         ctx.body = { status: "0000" }
//         ctx.body.data = result;
//     } catch (error) {
//         console.log("getBalance error===>", error);
//     }
//     next();
// }

module.exports = { deployedContract }